import {
    SYNC_TIME_INTERVAL,
    URLHEAD
} from "@/config/global"
import {
    Camera,
    POI,
    Path,
    Range,
    ShpRange,
    Heatmap,
    RoadHeatmap,
    MigrationMap
} from "@/lib/lotus"
import {
    busEventHandle,
    busEventDispatch,
    busEventEmit,
    windowEventHandle
} from "@/lib/extension"
import {
    render
} from "less"
import { sortedIndexOf } from "lodash"

export const _Space = {
    data() {
        return {
            Rendered: [],
            TimeState: null,
            CameraObj: null,
            realTimeTheme: false,
            camera: {
                init: null,
                current: null
            },
            cameraAutoRotate:true,
            colorList: [],
            autoTick: {
                whole: null
            }
        }
    },
    components: {
        'vw-left': () => import("@/layout/Left"),
        'vw-right': () => import("@/layout/Right"),
        'vw-row':()=>import("@/layout/Row"),
        'vw-box': () => import("@/layout/Box"),
        'vw-box-item': () => import("@/layout/BoxItem")
    },
    mounted() {
        this.InitCamera();
    },
    methods: {
        AutoSenseTimeState() {
            if (this.realTimeTheme) {
                this.SenseTimeSate();
                setInterval(() => {
                    this.SenseTimeSate();
                }, SYNC_TIME_INTERVAL);
            }
        },
        SenseTimeState() {
            let now = new Date();
            if (now.getHours() > 19 || now.getHours() < 7) {
                this.TimeState = "night";
            } else {
                this.TimeState = "day";
            }
        },
        CreateCamera(){
            this.CameraObj = new Camera();
        },
        InitCamera() {
            if(!this.CameraObj){
                this.CreateCamera();
            }
            this.InitCameraCallback();
        },
        InitCameraCallback() {

        },
        CameraRotateCancle() {

        },
        ClearSpace() {
            this.CameraObj.clear();
        },
        AutoRenderPanel(tickName, renderFn, interval) {
            renderFn();
            if (!this.autoTick[tickName]) {
                this.autoTick[tickName] = setInterval(() => {
                    renderFn();
                }, interval)
            }
        },
        AutoPagingRender(tickName, renderFn, interval) {
            //TODO:IMCOMPLETE
            let current = 0;
            renderFn();
        }
    },
    beforeDestroyed() {
        let keys = Object.keys(this.autoTick);
        keys.forEach(o => {
            clearInterval(this.autoTick[o]);
        })
    }
}
export const _POI = {
    created() {
        this.PoiClickEventListen();
        this.PoiWindowEventListen();
    },
    methods: {
        PoiWindowEventListen() {
            windowEventHandle(URLHEAD.attach, msg => {
                switch (msg.data.eventType) {
                    case 'poi-migrate-by-script':
                        this.POIMigrateByScript(msg.data.elementId);
                        break;
                    case 'poi-move-by-script':
                        this.POIMoveByScript(msg.data.elementId);
                        break;
                }
            })
        },
        PoiClickEventListen() {
            busEventHandle("poi-clicked", (msg) => {
                this.HandlePOIClicked(msg.id);
            });
        },
        HandlePOIClicked(id) {
            let poi = this.Rendered.find((e) => {
                return e.data.id === id;
            });
            poi.focus();
            // 在数据转换层，应当声明poi点是否会进入子路由以及子路由的名称，不在视图层去做判断。
            if (poi.data.viewMode === 'view') {
                this.$router.push({
                    name: poi.data.viewName,
                    params: {
                        id: poi.id
                    }
                });
            } else if (poi.data.viewMode === 'match') {
                if (this.matchPOI) {
                    //匹配类型的，需要组件用户自己重载一个matchPOI函数，注意参数是点击的poi，函数要返回被匹配的点，并且bindOn要做改变
                    this.matchPOI();
                    this.Rendered = this.Rendered.concat(this.matchPOI(poi));
                }
            }
        },
        RenderPOI(promise, params) {
            promise(params).then(v => {
                let poiGroup = v.map(o => {
                    let poi = new POI(o);
                    return poi;
                });
                this.Rendered = this.Rendered.concat(poiGroup);
                console.dir(this.Rendered);
                //this.CameraObj.focusOn("POI");
            })
        },
        RenderSinglePOI(promise, params) {
            promise(params).then(v => {
                let poi = new POI(v);
                poi.focus();
                this.Rendered.push(poi);
            })
        },
        RemovePOI(bindOnName) {
            this.Rendered.forEach((o, i) => {
                if (o.data.bindOn === bindOnName) {
                    o.remove();
                }
            });
            this.Rendered = this.Rendered.filter((o) => {
                return o.data.bindOn !== bindOnName;
            });
        },
        POIMoveByScript(id) {
            //开放给组件复写
        },
        POIMigrateByScript(id) {
            //开放给组件复写
        },
        matchPOI() {
            //开放给组件复写
        }
    }
}

export const _Path = {
    data() {
        return {}
    },
    created() {
        this.PathClickEventListen();
    },
    methods: {
        PathClickEventListen() {
            busEventHandle("path-clicked", (msg) => {
                this.HandlePathClicked(msg.id);
            });
        },
        HandlePathClicked(id) {
            let path = this.Rendered.find((e) => {
                return e.id === id;
            });
            path.focus();
        },
        RenderPath(promise, params) {
            promise(params).then(v => {
                let pathGroup = v.map(o => {
                    let path = new Path(o);
                    return path;
                });
                this.Rendered = this.Rendered.concat(pathGroup);
                if (pathGroup[0].data.poiList || pathGroup[0].data.poiList.length > 0) {
                    pathGroup.forEach(o => {
                        o.data.poiList.forEach(n => {
                            this.Rendered.push(new POI(n));
                        })
                    })
                }
                this.CameraObj.focusOn("path");
            })
        },
        RemovePath(bindOnName) {
            this.Rendered.forEach((o, i) => {
                if (o.data.bindOn === bindOnName) {
                    o.remove();
                }
            });
            this.Rendered = this.Rendered.filter((o) => {
                return o.data.bindOn !== bindOnName;
            });
        }
    }
}

export const _Range = {
    data() {
        return {}
    },
    created() {
        this.RangeClickEventListen();
    },
    methods: {
        RangeClickEventListen() {
            busEventHandle("range-clicked", (msg) => {
                this.HandleRangeClicked(msg.id);
            });
        },
        HandleRangeClicked(id) {
            let range = this.Rendered.find((e) => {
                return e.id === id;
            });
            range.focus();
        },
        RenderRange(promise, params) {
            promise(params).then(v => {
                let rangeGroup = v.map(o => {
                    let range = new Range(o);
                    return range;
                });
                this.Rendered = this.Rendered.concat(rangeGroup);
                if (v[0].poiList) {
                    v.forEach(o => {
                        o.poiList.forEach(n => {
                            this.Rendered.push(new POI(n));
                        })
                    })
                }
                this.CameraObj.focusOn("range")
            })
        },
        RemoveRange(bindOnName) {
            this.Rendered.forEach((o, i) => {
                if (o.data.bindOn === bindOnName) {
                    o.remove();
                }
            });
            this.Rendered = this.Rendered.filter((o) => {
                return o.data.bindOn !== bindOnName;
            });
        }
    }
}
export const _ShpRange = {
    data() {
        return {}
    },
    created() {
        this.ShpRangeClickEventListen();
    },
    methods: {
        ShpRangeClickEventListen() {
            busEventHandle("range-clicked", (msg) => {
                this.HandleShpRangeClicked(msg.id);
            });
        },
        HandleShpRangeClicked(id) {
            let range = this.Rendered.find((e) => {
                return e.id === id;
            });
            range.focus();
        },
        RenderShpRange(promise, params) {
            promise(params).then(v => {
                let shpRangeGroup = v.map(o => {
                    let shpRange = new ShpRange(o);
                    return shpRange;
                });
                this.Rendered = this.Rendered.concat(shpRangeGroup);

                this.CameraObj.focusOn("range")
            })
        },
        RemoveShpRange(bindOnName) {
            this.Rendered.forEach((o, i) => {
                if (o.data.bindOn === bindOnName) {
                    o.remove();
                }
            });
            this.Rendered = this.Rendered.filter((o) => {
                return o.data.bindOn !== bindOnName;
            });
        }
    }
}
export const _Heatmap = {
    data() {
        return {}
    },
    created() {
        this.HeatmapClickEventListen();
    },
    methods: {
        HeatmapClickEventListen() {
            busEventHandle("heatmap-clicked", (msg) => {
                this.HandleHeatmapClicked(msg.id);
            });
        },
        HandleHeatmapClicked(id) {
            let heatmap = this.Rendered.find((e) => {
                return e.id === id;
            });
            heatmap.focus();
        },
        RenderHeatmap(promise, params) {
            promise(params).then(v => {
                let heatmapGroup = v.map(o => {
                    let heatmap = new Heatmap(o);
                    return heatmap;
                });
                this.Rendered = this.Rendered.concat(heatmapGroup);
                this.CameraObj.focusOn("heatmap")
            })
        },
        RemoveHeatmap(bindOnName) {
            this.Rendered.forEach((o, i) => {
                if (o.data.bindOn === bindOnName) {
                    o.remove();
                }
            });
            this.Rendered = this.Rendered.filter((o) => {
                return o.data.bindOn !== bindOnName;
            });
        }
    }
}

export const _RoadHeatmap = {
    data() {
        return {}
    },
    created() {
        this.RoadheatmapClickEventListen();
    },
    methods: {
        RoadheatmapClickEventListen() {
            busEventHandle("roadheatmap-clicked", (msg) => {
                this.HandleRoadHeatmapClicked(msg.id);
            });
        },
        HandleRoadHeatmapClicked(id) {
            let roadheatmap = this.Rendered.find((e) => {
                return e.id === id;
            });
            roadheatmap.focus();
        },
        RenderRoadHeatmap(promise, params) {
            promise(params).then(v => {
                let roadheatmapGroup = v.map(o => {
                    let roadheatmap = new Heatmap(o);
                    return roadheatmap;
                });
                this.Rendered = this.Rendered.concat(roadheatmapGroup);
                this.CameraObj.focusOn("roadheatmap")
            })
        },
        RemoveRoadHeatmap(bindOnName) {
            this.Rendered.forEach((o, i) => {
                if (o.data.bindOn === bindOnName) {
                    o.remove();
                }
            });
            this.Rendered = this.Rendered.filter((o) => {
                return o.data.bindOn !== bindOnName;
            });
        }
    }
}

export const _Migrationmap = {
    data() {
        return {}
    },
    created() {
        this.MigrationmapClickEventListen();
    },
    methods: {
        MigrationmapClickEventListen() {
            busEventHandle("roadheatmap-clicked", (msg) => {
                this.HandleMigrationmapClicked(msg.id);
            });
        },
        HandleMigrationmapClicked(id) {
            let migrationMap = this.Rendered.find((e) => {
                return e.id === id;
            });
            migrationMap.focus();
        },
        RenderMigrationmap(promise, params) {
            promise(params).then(v => {
                let migrationMapGroup = v.map(o => {
                    let migrationMap = new MigrationMap(o);
                    return migrationMap;
                });
                this.Rendered = this.Rendered.concat(migrationMapGroup);
                this.CameraObj.focusOn("migrationmap")
            })
        },
        RemoveMigrationmap(bindOnName) {
            this.Rendered.forEach((o, i) => {
                if (o.data.bindOn === bindOnName) {
                    o.remove();
                }
            });
            this.Rendered = this.Rendered.filter((o) => {
                return o.data.bindOn !== bindOnName;
            });
        }
    }
}

export const _ToggleList = {
    data(){
        return{
            pageStatus:[]
        }
    },
    watch:{
        pageStatus:{
            handler(n,o){
                let pageStatusName = n[0];
                if(n){
                    //根据取到的pageStatusName进入不同的页面状态
                }else{ 
                    //执行方法，页面回到初始状态
                }
            },
            immediate:true,
            deep:true
        }
    },
    methods:{
        routePageStatus() {
            this.pageStatus = [this.pageStatus.tail()];
        }
    }
}